# minimum required version of cmake ...
CMAKE_MINIMUM_REQUIRED( VERSION 2.6.0 )

# name of the entire project ...
PROJECT( VisionControlServer )

## try to find and load the settings from an external project:
# assume, that the file "FindOpenCV.cmake" is in one of the CMAKE_MODULE_PATH folders.
FIND_PACKAGE( OpenCV REQUIRED )

###########################################################################
## Actual config file starts here
###########################################################################
IF(OpenCV_FOUND)
	MESSAGE( STATUS "OpenCV package found ..." )
	# information about the package libraries ... 
	MESSAGE( STATUS "Link Directory: \"${OpenCV_LINK_DIRECTORIES}\"" )
	MESSAGE( STATUS "Libraries: \"${OpenCV_LIBRARIES}\"" )
	# information about the system ...
	MESSAGE( STATUS "Host System: \"${CMAKE_SYSTEM}\"" )
	MESSAGE( STATUS "CPU Type: \"${CMAKE_HOST_SYSTEM_PROCESSOR}\"" )

	## configuration and generation:
	MESSAGE( STATUS "Try to create the configurations and the makefile ..." )
	# UNIX OS and UNIX like systems (including Mac OS X and Cygwin) ...
	IF(UNIX)
		# if compiler is a variant of g++ ...
		IF(CMAKE_COMPILER_IS_GNUCXX)
			IF(APPLE) # Mac OS X ...
				# add compiler flags for compiling the C sources (optimization instructions) ...
				# (note: the parameter "native" for detecting the host architecture does not exist
				# in the apple gcc/c++ compiler.)
				SET( CMAKE_CXX_FLAGS_RELEASE "-O2 -march=${CMAKE_HOST_SYSTEM_PROCESSOR} -mtune=${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "Flags used by the compiler during release builds." FORCE)
			ELSE(APPLE)
				SET( CMAKE_CXX_FLAGS_RELEASE "-O2 -march=native -mtune=native" CACHE STRING "Flags used by the compiler during release builds." FORCE)
			ENDIF(APPLE)
		ENDIF(CMAKE_COMPILER_IS_GNUCXX)

		# if compiler is a variant of gcc ...
		IF(CMAKE_COMPILER_IS_GNUCC)
			IF(APPLE)
				SET(CMAKE_C_FLAGS_RELEASE "-O2 -march=${CMAKE_HOST_SYSTEM_PROCESSOR} -mtune=${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "Flags used by the compiler during release builds." FORCE)
			ELSE(APPLE)
				SET( CMAKE_C_FLAGS_RELEASE "-O2 -march=native -mtune=native" CACHE STRING "Flags used by the compiler during release builds." FORCE)
			ENDIF(APPLE)
		ENDIF(CMAKE_COMPILER_IS_GNUCC)
	ENDIF(UNIX)

	# Windows System using Microsoft Visual C/C++ compiler ...
	IF(WIN32)
		IF(MSVC AND (${MSVC_VERSION} GREATER "1299")) # version 1300 is MSVC 6.0 ...
			IF("${CMAKE_C_COMPILER}" MATCHES "cl")
				SET( CMAKE_C_FLAGS_RELEASE "/MT /O2" )
			ENDIF("${CMAKE_C_COMPILER}" MATCHES "cl")
		
			IF("${CMAKE_CXX_COMPILER}" MATCHES "cl")
				SET( CMAKE_CXX_FLAGS_RELEASE "/MT /O2" )
			ENDIF("${CMAKE_CXX_COMPILER}" MATCHES "cl")
		ENDIF(MSVC AND (${MSVC_VERSION} GREATER "1299"))
	ENDIF(WIN32)

	# define the build type ...
	IF(NOT CMAKE_BUILD_TYPE)
		SET( CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug or Release." FORCE)
	ENDIF(NOT CMAKE_BUILD_TYPE)

	# create output folders for the executable targets ...
	# release version ...
	IF(${CMAKE_BUILD_TYPE} MATCHES "Release")
		SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/Release )
	ENDIF(${CMAKE_BUILD_TYPE} MATCHES "Release")
	# debug version ...
	IF(${CMAKE_BUILD_TYPE} MATCHES "Debug")
		SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/Debug )
	ENDIF(${CMAKE_BUILD_TYPE} MATCHES "Debug")

	# search for all sources (.c and .h files) in the current directory and add
	# them to the current project (generate a list of all files that are matching
	# to the "globbing" expressions) ...
	FILE( GLOB folder_source "*.c" )
	FILE( GLOB folder_header "*.h" )
	# define groups in which the sources will be placed ...
	SOURCE_GROUP( "Sources" FILES ${folder_source} )
	SOURCE_GROUP( "Headers" FILES ${folder_header} )
	# add the required include directories to the build ...
	INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR} ${OPENCV_INCLUDE_DIR} )
	
	# create the project: adding an executable target (vision_control_server) to
	# the project using the specified source files ...
	ADD_EXECUTABLE( vision_control_server ${folder_source} ${folder_header} )
	
	# link finally the executable target to a list of specified libraries (OpenCV library) ...
	TARGET_LINK_LIBRARIES( vision_control_server optimized ${OPENCV_LIBRARIES} )
ELSE(OpenCV_FOUND)
		# for convience. when the OpenCV library cannot be found, inform the user ...
    MESSAGE( SEND_ERROR "ERROR: OpenCV library not found!" )
ENDIF(OpenCV_FOUND)
